<?php
//include_once "includes.php";

class ActiveRecord {
	public $id;

	public function ActiveRecord ($params = null) {
		$this->__cargarParametros ($params);
	}

	public function __cargarParametros ($params = null) {
		if (!$params) return;

		foreach ($params as $key => $value) {
			$this->$key = $value;
		}

	}

	protected function getClass() {
		return get_class($this);
	}
	protected function getClassDB() {
		$word = get_class($this);
		$pluralRules = array('/([aeiou])$/i' => '\1s',
								'/$/' => 'es');

		foreach ($pluralRules as $rule => $replacement) {
			if (preg_match($rule, $word)) {
				$respuesta = preg_replace($rule, $replacement, $word);
				return $respuesta;
			}
		}
		return $word . "s";

	}

	public function findAll() {
		$tabla = $this->getClassDB();
		$sql = "SELECT * FROM $tabla";
		return queryObjectList($sql, $this->getClass());
	}

	public function queryList($sql) {
		return queryObjectList($sql, $this->getClass());
	}

	public function find ($id) {
		$sql = "SELECT * FROM " . $this->getClassDB() . " WHERE id=$id";
		return queryObject($sql, $this->getClass());
	}

	public function delete() {
		$sql = "DELETE FROM " . $this->getClassDB() . " WHERE id=$this->id";
		return execute($sql);
	}

	public function save ($params = null) {
		if (!$this->id) {
			$rta = $this->insert($params);
		} else {
			$rta = $this->update($params);
		}
		$this->actualizarTimestamps();
		return $rta;
	}

	// TODO: setear primero los Timestamps y luego hacer INSERT/UPDATE
	public function actualizarTimestamps () {
		$created = property_exists($this, 'created_at');
		$updated = property_exists($this, 'updated_at');
		if ($created || $updated) {
			$obj = $this->find ($this->id);
			if ($created) {
				$this->created_at = $obj->created_at;
			}
			if ($updated) {
				$this->updated_at = $obj->updated_at;
			}
		}

	}

	public function __call ($name, $args) {
		if (strpos($name, "find_by_") !== false) {
			return $this->find_by($name, $args);
		}
		if (strpos($name, "find_one_by_") !== false) {
			return $this->find_one_by($name, $args);
		}
		if (strpos($name, "find_like_") !== false) {
			return $this->find_like($name, $args);
		}

		$trace =& new Trace();
		echo "<div style='border:2px solid red;margin:4px;padding:4px'>Call to undefined method: <tt style='background:#FCC;padding:2px'>". $this->getClass() . "-&gt;" . htmlentities ($name) . "()</tt> in <b>$trace->file (line $trace->line)</b></div>";
		return false;
	
	}

	private function find_by ($name, $args) {
		$campo = substr($name, 8);
		$valor = $args[0];
		$tabla = $this->getClassDB();
		$sql = "SELECT * FROM $tabla
				WHERE $campo = '$valor'";
		return queryObjectList($sql, $this->getClass());
	} 

	private function find_one_by ($name, $args) {
		$campo = substr($name, 12);
		$valor = $args[0];
		$tabla = $this->getClassDB();
		$sql = "SELECT * FROM $tabla
				WHERE $campo = '$valor'
				LIMIT 1";

		return queryObject($sql, $this->getClass());
	} 

	// TODO: hacer una funcion para SELECT %LIKE%
	// (mejorar esto)
	private function find_like ($name, $args) {
		$campo = substr($name, 10);
		$valor = $args[0];
		$tabla = $this->getClassDB();
		$sql = "SELECT * FROM $tabla
				WHERE $campo LIKE '$valor'";
		return queryObjectList($sql, $this->getClass());
	} 

}

?>